package com.onlinegroup.controller;

import com.onlinegroup.entity.User;
import com.onlinegroup.service.UserService;
import com.onlinegroup.util.StringUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 首页Controller层
 */
@Controller
public class IndexController implements CommandLineRunner, ServletContextListener {

    @Override
    public void run(String... args) throws Exception {

    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        application = sce.getServletContext();
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

    private ServletContext application;

    @Resource
    private UserService userService;


    /**
     * 首页地址
     *
     * @return
     */
    @RequestMapping("/")
    public ModelAndView root(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "articleTypeId", required = false) String articleTypeId) {
//        this.loadSomeData();
        ModelAndView mav = new ModelAndView();
        if (page == null) {
            page = 1;
        }
        int pageSize = 10;
        Map<String, Object> map = new HashMap<>(16);
        map.put("start", (page - 1) * pageSize);
        map.put("size", pageSize);
        map.put("state", 2);
        map.put("sortByPublishDate", 1);
        StringBuffer param = new StringBuffer();
        if (StringUtil.isNotEmpty(articleTypeId)) {
            param.append("&articleTypeId=" + articleTypeId);
            map.put("articleTypeId", articleTypeId);
            mav.addObject("articleTypeId", articleTypeId);
        }
        mav.addObject("title", "首页");
        mav.addObject("mainPage", "page/indexFirst");
        mav.addObject("mainPageKey", "#b");
        mav.setViewName("index");
        return mav;
    }

    /**
     * 跳转到用户登录页面
     *
     * @return
     */
    @RequestMapping("/toLoginPage")
    public ModelAndView toLoginPage(HttpSession session) {
        User currentUser = (User) session.getAttribute("currentUser");
        if (currentUser != null) {
            ModelAndView mav = new ModelAndView("redirect:/");
            return mav;
        } else {
            ModelAndView mav = new ModelAndView();
            mav.addObject("title", "用户登录");
            mav.addObject("mainPage", "page/login");
            mav.addObject("mainPageKey", "#b");
            mav.setViewName("index");
            return mav;
        }
    }

    /**
     * 跳转到用户注册页面
     *
     * @return
     */
    @RequestMapping("/toRegisterPage")
    public ModelAndView toRegisterPage(HttpSession session) {
        User currentUser = (User) session.getAttribute("currentUser");
        if (currentUser != null) {
            ModelAndView mav = new ModelAndView("redirect:/");
            return mav;
        } else {
            ModelAndView mav = new ModelAndView();
            mav.addObject("title", "用户注册");
            mav.addObject("mainPage", "page/register");
            mav.addObject("mainPageKey", "#b");
            mav.setViewName("index");
            return mav;
        }
    }

    /**
     * 跳转到找回密码页面
     *
     * @return
     */
    @RequestMapping("/toSearchPasswordPage")
    public ModelAndView toSearchPasswordPage() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("title", "找回密码");
        mav.addObject("mainPage", "page/searchPassword");
        mav.addObject("mainPageKey", "#b");
        mav.setViewName("index");
        return mav;
    }

    /**
     * 跳转到用户后台页面
     *
     * @return
     */
    @RequestMapping("/toUserBackstagePage")
    public ModelAndView toUserBackstagePage() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("title", "用户后台");
        mav.addObject("mainPage", "page/userBackstage");
        mav.addObject("mainPageKey", "#b");
        mav.setViewName("index");
        return mav;
    }

    /**
     * 跳转到个人信息页面
     *
     * @return
     */
    @RequestMapping("/toPersonMessagePage")
    public ModelAndView toPersonMessagePage() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("title", "个人信息");
        mav.addObject("mainPage", "page/personMessage");
        mav.addObject("mainPageKey", "#b");
        mav.setViewName("index");
        return mav;
    }

    /**
     * 跳转到修改个人信息页面
     *
     * @return
     */
    @RequestMapping("/toPersonMessageUpdatePage")
    public ModelAndView toPersonMessageUpdatePage() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("title", "个人信息修改");
        mav.addObject("mainPage", "page/updatePersonMessage");
        mav.addObject("mainPageKey", "#b");
        mav.setViewName("index");
        return mav;
    }

    /**
     * 登录请求
     *
     * @return
     */
    @RequestMapping("/login")
    public Object login(HttpSession session) {
        User user = userService.findByUserName("admin");
        session.setAttribute("currentUserNickName", user.getNickName());
        return "/login";
    }

    /**
     * 进入后台管理请求
     *
     * @return
     */
    @RequestMapping("/admin")
    public String toAdmin() {
        return "/admin/main";
    }

    /**
     * 验证码是否正确
     *
     * @param imageCode
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkCodeIsSuccess")
    public Map<String, Object> checkCodeIsSuccess(String imageCode, HttpSession session) {
        Map<String, Object> resultMap = new HashMap<>(16);
        String checkCode = (String) session.getAttribute("checkCode");
        resultMap.put("checkCode", checkCode);
        if (StringUtil.isNotEmpty(checkCode)) {
            if (imageCode.equals(checkCode)) {
                resultMap.put("success", true);
            } else {
                resultMap.put("success", false);
            }
        }
        return resultMap;
    }
}
